import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeleteResult, Repository, UpdateResult } from 'typeorm';
import { Bk_Undergraduate } from './bk_undergraduate.entity';

@Injectable()
export class UndergraduateService {
    constructor(
        @InjectRepository(Bk_Undergraduate)
        private readonly undergraduateRepository: Repository<Bk_Undergraduate>
    ) { }

    async add(undergraduate: any): Promise<Bk_Undergraduate> {
        return await this.undergraduateRepository.save(undergraduate);
    }

    async delete(id: number): Promise<DeleteResult> {
        return await this.undergraduateRepository.delete(id);
    }

    async update(id: number, undergraduate: any): Promise<UpdateResult> {
        return await this.undergraduateRepository.update(id, undergraduate);
    }

    async findAll(query: any): Promise<[Bk_Undergraduate[], number]> {
        return await this.undergraduateRepository.findAndCount({ order: { id: "DESC" }, skip: query.skip, take: query.take });
    }

    async getUndergraduatesByCondition(year: number, major_category_code: number): Promise<Bk_Undergraduate[]> {
        return await this.undergraduateRepository.find({ where: { year, major_category_code }, order: { id: "DESC" } });
    }
}